
#include <windows.h>

typedef struct _DISPLAY_SETTINGS
{
	BOOL  bSetorResume;
    DWORD Width;
    DWORD Height;
    DWORD Depth;
    DWORD Freq;
    DWORD ModeIndex;
} DISPLAY_SETTINGS, * PDISPLAY_SETTINGS, * LPDISPLAY_SETTINGS;

TCHAR *AllVIADisplayModes[134] = {
	TEXT("640x480x16-60Hz"),
	TEXT("640x480x8-60Hz"),
	TEXT("640x480x32-60Hz"),
	TEXT("800x600x16-60Hz"),
	TEXT("800x600x8-60Hz"),
	TEXT("800x600x32-60Hz"),
	TEXT("1024x768x16-60Hz"),
	TEXT("1024x768x8-60Hz"),
	TEXT("1024x768x32-60Hz"),
	TEXT("1280x1024x8-60Hz"),
	TEXT("1280x1024x16-60Hz"),
	TEXT("1280x1024x32-60Hz"),
	TEXT("1280x768x8-60Hz"),
	TEXT("1280x768x16-60Hz"),
	TEXT("1280x768x32-60Hz"),
	TEXT("1280x960x8-60Hz"),
	TEXT("1280x960x16-60Hz"),
	TEXT("1280x960x32-60Hz"),
	TEXT("640x400x8-85Hz"),
	TEXT("640x400x16-85Hz"),
	TEXT("640x400x32-85Hz"),
	TEXT("800x480x8-60Hz"),
	TEXT("800x480x16-60Hz"),
	TEXT("800x480x32-60Hz"),
	TEXT("848x480x8-60Hz"),
	TEXT("848x480x16-60Hz"),
	TEXT("848X480X32-60Hz"),
	TEXT("720x480x8-60Hz"),
	TEXT("720x480x16-60Hz"),
	TEXT("720x480x32-60Hz"),
	TEXT("720x576x8-60Hz"),
	TEXT("720x576x16-60Hz"),
	TEXT("720x576x32-60Hz"),
	TEXT("640x480x8-75Hz"),
	TEXT("640x480x16-75Hz"),
	TEXT("640x480x32-75Hz"),
	TEXT("800x600x8-75Hz"),
	TEXT("800x600x16-75Hz"),
	TEXT("800x600x32-75Hz"),
	TEXT("1024x768x8-75Hz"),
	TEXT("1024x768x16-75Hz"),
	TEXT("1024x768x32-75Hz"),
	TEXT("1280x1024x8-75Hz"),
	TEXT("1280x1024x16-75Hz"),
	TEXT("1280x1024x32-75Hz"),
	TEXT("640x480x8-85Hz"),
	TEXT("640x480x16-85Hz"),
	TEXT("640x480x32-85Hz"),
	TEXT("800x600x8-85Hz"),
	TEXT("800x600x16-85Hz"),
	TEXT("800x600x32-85Hz"),
	TEXT("1024x768x8-85Hz"),
	TEXT("1024x768x16-85Hz"),
	TEXT("1024x768x32-85Hz"),
	TEXT("1280x1024x8-85Hz"),
	TEXT("1280x1024x16-85Hz"),
	TEXT("1280x1024x32-85Hz"),
	TEXT("320x200x8-85Hz"),
	TEXT("320x200x16-85Hz"),
	TEXT("320x200x32-85Hz"),
	TEXT("320x240x8-85Hz"),
	TEXT("320x240x16-85Hz"),
	TEXT("320x240x32-85Hz"),
	TEXT("400x300x8-85Hz"),
	TEXT("400x300x16-85Hz"),
	TEXT("400x300x32-85Hz"),
	TEXT("512x384x8-85Hz"),
	TEXT("512x384x16-85Hz"),
	TEXT("512x384x32-85Hz"),
	TEXT("480x234x8-60Hz"),
	TEXT("480x234x16-60Hz"),
	TEXT("480x234x32-60Hz"),
	TEXT("1600x1200x8-60Hz"),
	TEXT("1600x1200x16-60Hz"),
	TEXT("1600x1200x32-60Hz"),
	TEXT("1600x1200x8-75Hz"),
	TEXT("1600x1200x16-75Hz"),
	TEXT("1600x1200x8-85Hz"),
	TEXT("1600x1200x16-85Hz"),
	TEXT("1280x720x8-60Hz"),
	TEXT("1280x720x16-60Hz"),
	TEXT("1280x720x32-60Hz"),
	TEXT("1920x1440x8-60Hz"),
	TEXT("1920x1440x16-60Hz"),
	TEXT("1920x1440x32-60Hz"),
	TEXT("1920x1080x8-60Hz"),
	TEXT("1920x1080x16-60Hz"),
	TEXT("1920x1080x32-60Hz"),
	TEXT("1360X768X8-60Hz"),
	TEXT("1360X768X16-60Hz "),
	TEXT("1360X768X32-60Hz"),
	TEXT("1400x1050x8-60Hz"),
	TEXT("1400x1050X16-60Hz"),
	TEXT("1400x1050X32-60Hz"),
	TEXT("2048x1536x8-60Hz"),
	TEXT("2048x1536x16-60Hz"),
	TEXT("2048x1536x32-60Hz"),
	TEXT("1366x768x8-60Hz"),
	TEXT("1366x768x16-60Hz"),
	TEXT("1366x768x32-60Hz"),
	TEXT("640x480x8-100Hz"),
	TEXT("640x480x16-100Hz"),
	TEXT("640x480x32-100Hz"),
	TEXT("640x480x8-120Hz"),
	TEXT("640x480x16-120Hz"),
	TEXT("640x480x32-120Hz"),
	TEXT("800x600x8-100Hz"),
	TEXT("800x600x16-100Hz"),
	TEXT("800x600x32-100Hz"),
	TEXT("800x600x8-120Hz"),
	TEXT("800x600x16-120Hz"),
	TEXT("800x600x32-120Hz"),
	TEXT("1024x768x8-100Hz"),
	TEXT("1024x768x16-100Hz"),
	TEXT("1024x768x32-100Hz"),
	TEXT("1440x900x8-60Hz"),
	TEXT("1440x900x16-60Hz"),
	TEXT("1440x900x32-60Hz"),
	TEXT("1680x1050x8-60Hz"),
	TEXT("1680x1050x16-60Hz"),
	TEXT("1680x1050x32-60Hz"),
	TEXT("1680x1050x8-75Hz"),
	TEXT("1680x1050x16-75Hz"),
	TEXT("1680x1050x32-75Hz"),
	TEXT("1680x1050x8-85Hz"),
	TEXT("1680x1050x16-85Hz"),
	TEXT("1680x1050x32-85Hz"),
	TEXT("1920x1200x8-60Hz"),
	TEXT("1920x1200x16-60Hz"),
	TEXT("1920x1200x32-60Hz"),
	TEXT("1920x1200x8-75Hz"),
	TEXT("1920x1200x16-75Hz"),
	TEXT("1920x1200x32-75Hz"),
	NULL
};


//__declspec( dllexport ) int  SetDisplayMode( int width, int height, int colorDepth,  int freq )
   __declspec( dllexport ) int __stdcall SetDisplayMode( int width, int height, int colorDepth,  int freq )
{
	HDC	  hDc;
	int	  i,iRetVal;
	int   iMode;
	TCHAR settings[100]={0};

	DISPLAY_SETTINGS testMode;
	    
	if ( colorDepth == 24 ) 
	{
		colorDepth = 32;
	}
	
	_stprintf(settings, TEXT("%dx%dx%d-%dHz"),width, height,colorDepth,freq);
	
	for ( i = 0; AllVIADisplayModes[i] != 0; i++ )
	{
		if ( _tcsicmp(AllVIADisplayModes[i], settings) == 0 )
		{
			iMode = i;
			break;
		}
	}

	testMode.bSetorResume= TRUE;
    testMode.Width  = width;
    testMode.Height = height;
    testMode.Depth  = colorDepth;
    testMode.Freq   = freq;
	testMode.ModeIndex = iMode;

	//return TRUE;
	hDc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);

	// Make the escape call to see if queryescape is supported.
	iRetVal = ExtEscape(hDc, 0X4103,
						sizeof(testMode),
						(LPSTR)&testMode,
						0,
						NULL);
	
	DeleteDC(hDc);

	if ( iRetVal )
	{
		Sleep(4000);
		testMode.bSetorResume = FALSE;
		//return TRUE;
		hDc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);

		// Make the escape call to see if queryescape is supported.
		iRetVal = ExtEscape(hDc, 0X4103,
							sizeof(testMode),
							(LPSTR)&testMode,
							0,
							NULL);
		
		DeleteDC(hDc);
	}

	if ( iRetVal != 1 )
	{
		return 0;
	}

	return 1;    
}



BOOL APIENTRY DllMain( HANDLE  hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

